"""
Author: zhuanglaihong
Date: 2025-04-03 14:27:40
LastEditTime: 2025-04-08 20:49:35
LastEditors: zhuanglaihong
FilePath: /zlh/rainfall/src/main.py
Copyright: Copyright (c) 2021-2024 zhuanglaihong. All rights reserved.
"""
import json
from pathlib import Path
import os
import glob
import xarray as xr
import numpy as np
import pandas as pd
import time

import calendar
from main import (
    reform_gfs_files,
    standardize_gfs_data,
    correct_gfs_forecast,
    save_corrected_gfs,
    save_params_data
)
from core.config import *
from path_config import *
from loguru import logger

ROOT_DIR = Path(__file__).parent.parent.absolute()

def monitor_gfs_folder(start_year: int, start_month: int):
    """监听GFS文件夹，发现新文件时进行处理
    
    Args:
        start_year: 开始监听的年份
        start_month: 开始监听的月份
    """
    # 基础路径设置
    base_gfs_path = "/app/minio/gfs-origin/GFS/CHN-FORECASAT/1h"
    source_folder = os.path.join(ROOT_DIR,"/app/minio/rainfall/A_OA_data/GFS/params")
    params_folder = os.path.join(ROOT_DIR, "data/params")
    minio_folder = os.path.join(ROOT_DIR, "/app/minio/rainfall/A_OA_data/GFS/1h_revise")
    
    # 初始化当前年月
    current_year = start_year
    current_month = start_month
    
    # 使用文件持久化存储处理记录
    processed_record_file = os.path.join(ROOT_DIR, "data/processed_folders.json")
    
    # 加载已处理的记录
    if os.path.exists(processed_record_file):
        logger.info(f"加载已处理记录文件: {processed_record_file}")
        with open(processed_record_file, 'r') as f:
            processed_folders = json.load(f)
        logger.info(f"成功加载 {len(processed_folders)} 条处理记录")
    else:
        logger.info(f"处理记录文件不存在，创建新的记录: {processed_record_file}")
        processed_folders = {}
    
    while True:
        try:
            # 获取当前年月的文件夹路径
            year_month_path = os.path.join(base_gfs_path, f"{current_year}/{current_month:02d}")
            
            if os.path.exists(year_month_path):
                # 获取当月天数
                days_in_month = calendar.monthrange(current_year, current_month)[1]
                
                # 获取当前月份已存在的日期文件夹
                existing_days = []
                for day in range(1, days_in_month + 1):
                    day_folder = os.path.join(year_month_path, str(day).zfill(2))
                    if os.path.exists(day_folder):
                        existing_days.append(day)
                
                if existing_days:
                    logger.info(f"{current_year}年{current_month:02d}月已有数据的日期: {existing_days}")
                
                # 检查每一天的文件夹
                for day in existing_days:
                    day_folder = os.path.join(year_month_path, str(day).zfill(2))
                    
                    # 检查是否已处理过该日期
                    if day_folder not in processed_folders:
                        logger.info(f"发现新数据: {current_year}年{current_month:02d}月{day:02d}日")
                        
                        # 设置处理路径
                        reform_gfs_folder = os.path.join(ROOT_DIR, f"data/minio/{current_year}_{current_month:02d}_gfs_grb2")
                        standard_gfs_folder = os.path.join(ROOT_DIR, f"data/minio/{current_year}_{current_month:02d}_standard")
                        corrected_gfs_folder = os.path.join(ROOT_DIR, f"data/{current_year}_{current_month:02d}_calibrated")
                        
                        # 处理该日期文件夹
                        reform_gfs_files(day_folder, reform_gfs_folder)
                        
                        # 更新并保存处理记录
                        processed_folders[day_folder] = True  # 使用布尔值标记已处理
                        with open(processed_record_file, 'w') as f:
                            json.dump(processed_folders, f)
                        
                        # 执行后续处理
                        logger.info("开始标准化GFS数据")
                        standardize_gfs_data(reform_gfs_folder, standard_gfs_folder, reference_nc_file)
                        
                        logger.info("基于参数文件对GFS数据订正")
                        save_params_data(source_folder, params_folder)
                        correct_gfs_forecast(standard_gfs_folder, params_folder, corrected_gfs_folder)
                        
                        logger.info("上传GFS数据至MinIO")
                        save_corrected_gfs(corrected_gfs_folder, minio_folder)
            
            # 更新年月
            current_month += 1
            if current_month > 12:
                current_month = 1
                current_year += 1
            
            # 休眠一段时间再检查
            time.sleep(300)  # 5分钟检查一次
            
        except Exception as e:
            logger.error(f"监听处理出错: {e}")
            time.sleep(60)  # 出错后等待1分钟再继续
            continue

def main():
    """自动化监听订正"""
    start_year = 2025
    start_month = 5
    try:
        monitor_gfs_folder(start_year, start_month)
    except KeyboardInterrupt:
        logger.info("程序已停止")
    except Exception as e:
        logger.error(f"程序执行出错: {e}")

if __name__ == "__main__":
    main()
